package com.zisu.springcloud.controller;

import com.zisu.springcloud.entity.CommonResult;
import com.zisu.springcloud.entity.Payment;
import com.zisu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2020/9/1 13:45
 */
@RestController
@Slf4j
public class PaymentController {
    @Autowired
    private PaymentService paymentService;

    //从yml 配置文件中读取 属性值
    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/payment/lb")
    public String getPaymentLB(){
        return serverPort;
    }

    @PostMapping(value = "/payment/create")
    public CommonResult<Integer> create(Payment payment){
        int save = paymentService.save(payment);
        log.info("插入结果："+save);
        if(save>=1){
            // 主要是用来看 调用了哪个服务
            return new CommonResult(200,"插入数据成功，，serverPort："+serverPort,save);
        }else {
            return new CommonResult(500,"插入数据失败",null);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> get(@PathVariable("id") Long id){
        Payment paymentById = paymentService.getPaymentById(id);
        System.out.println(12333);
        if(paymentById != null){
            return new CommonResult(200,"插入数据成功，，serverPort："+serverPort,paymentById);
        }else {
            return new CommonResult(500,"插入数据失败",null);
        }
    }

    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout(){
        try {
            TimeUnit.SECONDS.sleep(3);
        }catch (Exception e){
            e.printStackTrace();
        }
        return serverPort;
    }
}
